/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Last Change:
 *    by:   $Author: $
 *    date: $Date:   $
 * Copyright (c): Johannes Kuhaupt
 * Licence: Creative Commons CC BY-NC-SA 3.0
 */
package control.boo;

import control.GuiManager;
import control.SettingsManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Johannes Kuhaupt
 */
public class LejosCompiler extends AbstractJavaTask {

    File path;

    /**
     *
     * @param pFile
     */
    public LejosCompiler(String pFile) {
        this.taskFile = new File(pFile);
    }

    /**
     *
     */
    @Override
    public void run() {
        this.guiMan.setCompilerReady(false);
        this.startLejosTask();
    }

    private void startLejosTaskWithExtension() {
        String pathToBoo = SettingsManager.getStringSettingValue("PATH_TO_LEJOSCOMPILER");
        String pathToJar = SettingsManager.getStringSettingValue("PATH_TO_JAREXTENSION");
        File booExe = new File(pathToBoo);
        File jarFile = new File(pathToJar);
        if (booExe.exists() && booExe.canExecute() && (booExe.getName().equals("nxjc.bat") || booExe.getName().equals("nxjc")) && jarFile.exists() && jarFile.canRead() && jarFile.isFile()) {
            pb = new ProcessBuilder(pathToBoo, "-cp", pathToJar, taskFile.getName());
            pb.redirectErrorStream(true);
            pb.directory(path);

            try {
                currentProcess = pb.start();
                processStarted = true;
            } catch (IOException ex) {
                this.guiMan.setOutputInFrame(this.compilerResult);
                this.guiMan.setCompilerReady(true);
                this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorInterpreting"));
                Logger.getLogger(LejosCompiler.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Error, startBooTask failed (Interpreter): " + ex.toString());
            }
            if(processStarted){
                this.readOutputStream();            
            }
        }
        else{
            this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorBooc"));
            this.guiMan.setCompilerReady(true);            
        }

        
    }

    private void startLejosTaskWithoutExtension() {
        String pathToBoo = SettingsManager.getStringSettingValue("PATH_TO_LEJOSCOMPILER");
        File booExe = new File(pathToBoo);
        if (booExe.exists() && booExe.canExecute() && (booExe.getName().equals("nxjc.bat") || booExe.getName().equals("nxjc"))) {
            pb = new ProcessBuilder(pathToBoo, taskFile.getName());
            pb.redirectErrorStream(true);
            pb.directory(path);

            try {
                currentProcess = pb.start();
                processStarted = true;
            } catch (IOException ex) {
                this.guiMan.setOutputInFrame(this.compilerResult);
                this.guiMan.setCompilerReady(true);
                this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorInterpreting"));
                Logger.getLogger(LejosCompiler.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Error, startBooTask failed (Interpreter): " + ex.toString());
            }

            

        }
    }

    private void startLejosTask() {
        compilerResult = "";
        currentProcess = null;
        processStarted = false;

        path = new File(taskFile.getParent());
        String pathToJar = SettingsManager.getStringSettingValue("PATH_TO_JAREXTENSION");
        
        LejosFileMerger merge = new LejosFileMerger(this.taskFile);
        merge.buildTheWholeSourceCode();
        System.out.println("Hä?");

        if (pathToJar.isEmpty()) {
            this.startLejosTaskWithoutExtension();
        } else {
            this.startLejosTaskWithExtension();
        }
        
        if (processStarted) {
                this.readOutputStream();
                this.guiMan.setCompilerReady(true);
                if(this.compilerResult.equals("")){
                    this.guiMan.setOutputInFrame(GuiManager.getLangBundle().getString("MsgCompileSuccess"));
                    this.guiMan.setStatusInfo(GuiManager.getLangBundle().getString("StatusMsgCompileSuccess"));
                }
                else{
                    this.guiMan.setOutputInFrame(this.compilerResult);
                    this.guiMan.setStatusInfo(GuiManager.getLangBundle().getString("StatusMsgCompileError"));
                    this.guiMan.showOutputTab();
                }
                this.guiMan.setCompilerReady(true);
                
            } else { //muss angezeigt werden wenn oben Error kam weil exe nicht gefunden
                this.guiMan.showErrorMessage(GuiManager.getLangBundle().getString("MsgErrorBooi"));
                this.guiMan.setCompilerReady(true);
            }

    }
}
